﻿using System;

namespace SpiderMonkey
{
    public abstract class NativeObject : IDisposable
    {
        private IntPtr ptr;
        private bool delete;

        internal protected NativeObject()
        {
        }
        internal protected NativeObject(IntPtr ptr, bool delete = true)
        {
            Setup(ptr, delete);
        }
        ~NativeObject()
        {
            if (!delete) return;
            Dispose(false);
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
            if (!delete) return;
            Dispose(true);
        }
        internal protected void Setup(IntPtr ptr, bool delete = true)
        {
            this.ptr = ptr;
            this.delete = delete;
        }
        protected abstract void Dispose(bool disposing);

        internal IntPtr Pointer
        {
            get { return ptr; }
        }
    }
}
